from flask import g, Blueprint, redirect, url_for, request, jsonify, session, render_template, flash

from exts import db, get_time
from flask_login import current_user, login_user, logout_user, login_required
from decorations import teacher_required, root_required, student_required
from my_configs import online_programming_url, weixinpay_url
import requests

from models import File, Classes, User, Student, Teacher, Root, SystemLog

bp = Blueprint("visitor", __name__, url_prefix="/")


# ---------- 在线编程 ---------------
# todo url online_code 在线编程
@bp.route("/online_code", methods=["POST"])
@login_required
def online_code():
    code = request.values.get("code")
    result = requests.post(
        url=online_programming_url,
        data={"code": code}
    )
    return result.text


# todo online_programming
@bp.route("/online_programming")
@login_required
def online_programming():
    content = {
        "user": g.user,
    }
    return render_template("online_programming.html", **content)


# ---------------------------------
# -------------- 文件 --------------
# todo download 文件
@bp.route("/download")
@login_required
def download():
    content = {
        "user": g.user,
        "files": db.session.query(File).all()
    }
    return render_template("download.html", **content)


# todo upload
@bp.route("/upload")
@login_required
def upload():
    content = {
        "user": g.user,
    }
    return render_template("upload.html", **content)


# todo index_file
@bp.route('/index_file')
@login_required
def index_file():  # put application's code here
    content = {
        "user": g.user,
    }
    return render_template("index_file.html", **content)


# ---------------------------------------------
# todo url / 未登陆主界面
@bp.route("/")
def index():
    content = {
        "weixinpay_url": weixinpay_url
    }
    try:
        if g.user.power == 0:
            return redirect("/admin")
        elif g.user.power == 1:
            return redirect("/teacher")
        else:
            return redirect("/student")
    except:
        return render_template("index.html", **content)


# todo url /student 学生登陆主界面
@bp.route("/student")
@login_required
@student_required
def student():
    content = {
        "user": g.user,
    }
    return render_template("student/index.html", **content)


# todo url /teacher 教师登陆主界面
@bp.route("/teacher")
@login_required
@teacher_required
def teacher():
    content = {
        "user": g.user,
    }
    return render_template("teacher/index.html", **content)


# todo url /admin 管理员登陆主界面
@bp.route("/admin")
@login_required
@root_required
def admin():
    content = {
        "user": g.user,
        "xAxis_data": ['学生', '老师', '管理员'],
        "series_data": [len(db.session.query(Student).all()), len(db.session.query(Teacher).all()),
                        len(db.session.query(Root).all())]
    }
    return render_template("admin/index.html", **content)


# todo url /login 登陆界面
@bp.route("/login")
def login():
    content = {
    }
    return render_template("login.html", **content)
